<?php
/**
 * User: joedarkangel3
 * String modification,and validation
 * Date: 8/24/11
 * Time: 11:41 AM
 */

class validation_string {
    // only one type of error, coder's error
    private $isValid;
    private $data ="";
    public function __construct(){
        $this->data = "";
    }

    /**
     * reset data and valid status
     * we only accept STRING
     * @param mix $data
     * @return validation_string
     */
    public function set_data($data){
        $this->data = $data;
        $this->isValid = TRUE;
        /**
         * show error if not in production mode and data is not string
         */
        if(!is_string($data)){
            // make $data string to prevent problem in following validations, and set it to automatic fail
            $this->data = (string)$data;
            $this->isValid = FALSE;
        }
        return $this;
    }
    /**
     * check min length
     * @param int $length
     * @return validation_string
     */
    public function min_length($length){
        /**
         * show error if not in production mode on param mistake for taking not integer
         */
        if(!is_int($length)){
            /**
             * automatic fail
             */
            $this->isValid = FALSE;
        }
        else if(mb_strlen($this->data) < $length){
            $this->isValid = FALSE;
        }
        return $this;
    }

    /**
     * check max length
     * @param int $length
     * @return validation_string
     */
    public function max_length($length){
        /**
         * show error if not in production mode on param mistake for taking not integer
         */
        if(!is_int($length)){
            /**
             * automatic fail
             */
            $this->isValid = FALSE;
        }
        if(mb_strlen($this->data) > $length){
            $this->isValid = FALSE;
        }
        return $this;
    }
    /**
     * check exact length
     * @param $length
     * @return validation_string
     */
    public function exact_length($length){
        /**
         * show error if not in production mode on param mistake for taking not integer
         */
        if(!is_int($length)){
            /**
             * automatic fail
             */
            $this->isValid = FALSE;
        }
        if(mb_strlen($this->data) != $length){
            $this->isValid = FALSE;
        }
        return $this;
    }

    /**
     * natural number 0, 1, 2, ...
     * @return vlaidation_string
     */
	function is_natural()
	{
		$result = (bool) preg_match( '/^[0-9]+$/', $this->data);
        if(FALSE == $result){
            $this->isValid = FALSE;
        }
        return $this;
	}
    /**
     * validate agaisnt usa zipcode
     * @return validation_string
     */
    function is_us_zipcode(){
        $this->max_length(10);
        // if fail length test, should return now in case digi pass test result in tampering the test
        if($this->result() === FALSE){
            return $this;
        }
        else{
            $zipcode_digi_test = str_replace('-', '', $this->data);
            $this->set_data($zipcode_digi_test)->is_natural();
            return $this;
        }
    }
    /**
     * sequence could be any combination of digits and '-'
     * @return validation_string
     */
    function is_sequence(){
        $this->max_length(8);
        // if fail length test, should return now in case digi pass test result in tampering the test
        if($this->result() === FALSE){
            return $this;
        }
        else{
            $sequence_digi_test = str_replace('-', '', $this->data);
            $this->set_data($sequence_digi_test)->is_natural();
            return $this;
        }
    }
    /**
     * @return boolean
     */
    public function result(){
        return $this->isValid;
    }
}
